package com.syezon.wifikey.notification;

import android.content.Context;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

import com.syezon.android.base.download.DownloadTask;
import com.syezon.wifikey.R;
import com.syezon.wifikey.utils.FileSizeUtil;

public class DownloadNotification {
    private static DownloadNotification mDownloadNotification;
    private NotificationManagerCompat mNotificationManagerCompat;
    private Context mContext;

    private DownloadNotification(Context context) {
        this.mContext = context;
        init();
    }

    public static DownloadNotification getDownloadNotification(Context context) {
        if (mDownloadNotification == null) {
            mDownloadNotification = new DownloadNotification(context);
        }
        return mDownloadNotification;
    }

    private void init() {
        mNotificationManagerCompat = NotificationManagerCompat.from(mContext);
    }

    public void notifyDownloadStart(DownloadTask downloadTask, String title) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentText("下载开始！");
        notificationBuilder.setContentTitle(title);
        mNotificationManagerCompat.notify(downloadTask.getFileName().hashCode(), notificationBuilder.build());
    }

    public void notifyDownloadProgress(DownloadTask downloadTask, String title) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentText("下载进度：" + FileSizeUtil.formatFileSize(downloadTask.getCompletedSize(), FileSizeUtil.SIZE_TYPE_MB) + "M" + "/" + FileSizeUtil.formatFileSize(downloadTask.getTotalSize(), FileSizeUtil.SIZE_TYPE_MB) + "M");
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setProgress((int) downloadTask.getTotalSize(), (int) downloadTask.getCompletedSize(), false);
        mNotificationManagerCompat.notify(downloadTask.getFileName().hashCode(), notificationBuilder.build());
    }

    public void notifyDownloadPause(DownloadTask downloadTask, String title) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentText("下载暂停：" + FileSizeUtil.formatFileSize(downloadTask.getCompletedSize(), FileSizeUtil.SIZE_TYPE_MB) + "M" + " :" + FileSizeUtil.formatFileSize(downloadTask.getTotalSize(), FileSizeUtil.SIZE_TYPE_MB) + "M");
        notificationBuilder.setContentTitle(title);
        mNotificationManagerCompat.notify(downloadTask.getFileName().hashCode(), notificationBuilder.build());
    }

    public void notifyDownloadCancel(DownloadTask downloadTask, String title) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentText("下载取消：" + FileSizeUtil.formatFileSize(downloadTask.getCompletedSize(), FileSizeUtil.SIZE_TYPE_MB) + "M" + "/" + FileSizeUtil.formatFileSize(downloadTask.getTotalSize(), FileSizeUtil.SIZE_TYPE_MB) + "M");
        notificationBuilder.setContentTitle(title);
        mNotificationManagerCompat.notify(downloadTask.getFileName().hashCode(), notificationBuilder.build());
    }

    public void notifyDownloadFinish(DownloadTask downloadTask, String title) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentText("下载完成：" + FileSizeUtil.formatFileSize(downloadTask.getCompletedSize(), FileSizeUtil.SIZE_TYPE_MB) + "M" + "/" + FileSizeUtil.formatFileSize(downloadTask.getTotalSize(), FileSizeUtil.SIZE_TYPE_MB) + "M");
        notificationBuilder.setContentTitle(title);
        mNotificationManagerCompat.notify(downloadTask.getFileName().hashCode(), notificationBuilder.build());
    }

    public void notifyDownloadError(DownloadTask downloadTask, String title, int errorCode) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentText("下载失败：" + FileSizeUtil.formatFileSize(downloadTask.getCompletedSize(), FileSizeUtil.SIZE_TYPE_MB) + "M" + "/" + FileSizeUtil.formatFileSize(downloadTask.getTotalSize(), FileSizeUtil.SIZE_TYPE_MB) + "M");
        notificationBuilder.setContentTitle(title);
        mNotificationManagerCompat.notify(downloadTask.getFileName().hashCode(), notificationBuilder.build());
    }
}